package query;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.jdo.JDOHelper;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.cinema.data.Adminusr;
import com.cinema.data.FilmInfo;
import com.cinema.data.PMF;
import com.cinema.data.Schedule;
import com.cinema.data.Ticket;
import com.cinema.data.User;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DataQuery {
	public boolean addUser(User usr){
		Key key = KeyFactory.createKey(User.class.getSimpleName(), usr.getUsername());
		usr.setID(key);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		boolean result = false;
		
		try{
			User test = pm.getObjectById(User.class, key);
		}catch(JDOObjectNotFoundException e){
			pm.makePersistent(usr);
			result = true;
		}finally{
			pm.close();
		}
		return result;
	}
	
	public boolean addAdminUser(Adminusr usr){
		Key key = KeyFactory.createKey(Adminusr.class.getSimpleName(), usr.getUsername());
		usr.setID(key);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		boolean result = false;
		
		try{
			Adminusr test = pm.getObjectById(Adminusr.class, key);
		}catch(JDOObjectNotFoundException e){
			pm.makePersistent(usr);
			result = true;
		}finally{
			pm.close();
		}
		return result;
	}
	
	public User checkUser(String username, String password){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(User.class.getSimpleName(), username);
		System.out.println("checkUser " + pm);
		User usr = null;
		try{
			usr = pm.getObjectById(User.class, k);
			if(!usr.getPassword().equals(password))
				usr = null;
		}catch(JDOObjectNotFoundException e){
			usr = null;
		}finally{
			pm.close();
		}
		return usr;
	}
	
	public User getUserByID(long ID){
		List<User> userlist = null;
		User user = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		System.out.println("getFilm " + pm);
		Query query = pm.newQuery(User.class);
		query.setFilter("uID == :fid_param");
		try{
			userlist = (List<User>)query.execute(ID);
			if(!userlist.isEmpty()){
				for(User f: userlist)
					user = f;
			}
		}finally{
			query.closeAll();
		}
		return user;
	}
	
	public Adminusr checkAdmin(String username, String password){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(Adminusr.class.getSimpleName(), username);
		System.out.println("checkUser " + pm);
		Adminusr usr = null;
		try{
			usr = pm.getObjectById(Adminusr.class, k);
			if(!usr.getPassword().equals(password))
				usr = null;
		}catch(JDOObjectNotFoundException e){
			usr = null;
		}finally{
			pm.close();
		}
		return usr;
	}
	
	public boolean addFilm(FilmInfo film){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Key k = KeyFactory.createKey(FilmInfo.class.getSimpleName(), film.getFname());
		film.setID(k);
		try{
			pm.makePersistent(film);
		}finally{
			pm.close();
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public List<FilmInfo> getCurrentFilmList(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		System.out.println("getCurrentFilmList " + pm);
		Query query = pm.newQuery(FilmInfo.class);
		List<FilmInfo> flist = null;
		List<FilmInfo> flist1 = null;
		query.declareImports("import java.util.Date");
		query.setFilter("today > :now");
		try{
			flist = (List<FilmInfo>)query.execute(new Date());
			if(flist.isEmpty())
				flist = null;
			else{
				flist1 = new ArrayList<FilmInfo>();
				for(FilmInfo f: flist){
					Date fromday = f.getFromday();
					if(fromday.before(new Date()))
						flist1.add(f);
				}
			}
		}finally{
			query.closeAll();
		}
		return flist1;
	}
	
	@SuppressWarnings("unchecked")
	public List<FilmInfo> getPlanFilmList(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		System.out.println("getCurrentFilmList " + pm);
		Query query = pm.newQuery(FilmInfo.class);
		List<FilmInfo> flist = null;
		query.declareImports("import java.util.Date");
		query.setFilter("fromday > :now");
		try{
			flist = (List<FilmInfo>)query.execute(new Date());
			if(flist.isEmpty())
				flist = null;
		}finally{
			query.closeAll();
		}
		return flist;
	}
	
	@SuppressWarnings("unchecked")
	public FilmInfo getFilm(long ID){
		List<FilmInfo> filmlist = null;
		FilmInfo film = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		System.out.println("getFilm " + pm);
		Query query = pm.newQuery(FilmInfo.class);
		query.setFilter("fid == :fid_param");
		try{
			filmlist = (List<FilmInfo>)query.execute(ID);
			if(!filmlist.isEmpty()){
				for(FilmInfo f: filmlist)
					film = f;
			}
		}finally{
			query.closeAll();
		}
		return film;
	}
	
	public void addTicket(Ticket t){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		System.out.println("addTicket " + pm);
		Random randomGenerator = new Random();
		int ticketid = randomGenerator.nextInt(Integer.MAX_VALUE);
		t.setTid(ticketid);
		Key k = KeyFactory.createKey(Ticket.class.getSimpleName(), t.getTid());
		t.setID(k);
		try{
			pm.makePersistent(t);
		}finally{
			pm.close();
		}
	}
	
	public void updateFilmSchedule(Key key, Schedule sched){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			FilmInfo film = pm.getObjectById(FilmInfo.class, key);
			film.setSchedule(sched);
		}finally{
			pm.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Ticket> getTicketList(long filmid){
		List<Ticket> ticket = null;
		FilmInfo film = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ticket.class);
		query.setFilter("filmid == :fid_param");
		try{
			ticket = (List<Ticket>)query.execute(filmid);
			if(ticket.isEmpty())
				ticket = null;
		}finally{
			query.closeAll();
		}
		return ticket;
	}
	
	public boolean removeFilm(long filmid){
		boolean result = false;
		FilmInfo film = getFilm(filmid);
		String filmname = film.getFname();
		Key k = KeyFactory.createKey(FilmInfo.class.getSimpleName(), filmname);
		if(film != null){
			PersistenceManager pm = PMF.get().getPersistenceManager();
			try{
				FilmInfo f = pm.getObjectById(FilmInfo.class, k);
				pm.deletePersistent(f);
			}finally{
				pm.close();
			}
			result = true;
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Adminusr> getAdminUserList(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		System.out.println("getCurrentFilmList " + pm);
		Query query = pm.newQuery(Adminusr.class);
		List<Adminusr> flist = null;
		try{
			flist = (List<Adminusr>)query.execute();
		}finally{
			query.closeAll();
		}
		return flist;
	}
	
	public boolean deleteAdminUser(String username){
		boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Adminusr.class);
		List<Adminusr> flist = null;
		query.setFilter("username == :usr_param");
		try{
			flist = (List<Adminusr>)query.execute(username);
			if(!flist.isEmpty()){
				result = true;
				for(Adminusr u: flist)
					pm.deletePersistent(u);
			}
		}finally{
			query.closeAll();
		}
		return result;
	}
}
